#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：back 
@File    ：notify_dao.py
@IDE     ：PyCharm 
@Author  ：七分是你
@Date    ：2024/6/29 下午12:48 
@explain : 
"""
from sqlalchemy import func

from app.dao.models import XlxNotifyLog, XlxNotifyRule
from . import tools
from .base_dao import getDatabaseSession


class NotifyQueryDao:
    @classmethod
    def get_notify_rules(cls) -> list[XlxNotifyRule]:
        with getDatabaseSession() as session:
            query = session.query(XlxNotifyRule)
            query = query.order_by(XlxNotifyRule.id)
            return query.all()

    @classmethod
    def find_notify_log(cls, **kwargs) -> list:
        with getDatabaseSession() as session:
            query = session.query(XlxNotifyLog.uid, func.count(XlxNotifyLog.id).label('num'))
            query = tools.build_query(query, XlxNotifyLog, **kwargs)
            query = query.group_by(XlxNotifyLog.uid)
            result = query.all()
        return result


class NotifyOperateDao(object):
    @classmethod
    def insert_all(cls, data: list[dict], mode_name: str = 'Log'):
        with getDatabaseSession() as session:
            models = XlxNotifyLog
            if mode_name == 'Rule':
                models = XlxNotifyRule
            log = [models(**inset_data) for inset_data in data]
            session.add_all(log)
